import Util from "./Util";

let cvs: WechatMiniprogram.Canvas;
let ctx: WechatMiniprogram.CanvasRenderingContext.CanvasRenderingContext2D;
let cacheCvs: WechatMiniprogram.OffscreenCanvas;
let cacheCtx: typeof ctx;

export default class CanvasRuntime {
  public width = wx.$system.screenWidth;
  public height = wx.$system.screenHeight;

  private utils = new Util();
  private async initial() {
    if (!cvs) {
      cvs = await this.utils.getCanvasNode(wx.$scm.stageId)
      cvs.width = this.width;
      cvs.height = this.height;
      ctx = cvs.getContext('2d')

      cacheCvs = wx.createOffscreenCanvas({type: '2d', width: this.width, height: this.height})
      cacheCtx = cacheCvs.getContext('2d');
    }
  }

  constructor() { this.initial(); }
}